home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / BARNET / COMPILER / SATHER / !Sather / Library / Base / sa / abstract next >
Text File  |  1996-06-02  |  7KB  |  186 lines

  1. ---------------------------> Sather 1.1 source file <--------------------------
  2. -- Copyright (C) International Computer Science Institute, 1994.  COPYRIGHT  --
  3. -- NOTICE: This code is provided "AS IS" WITHOUT ANY WARRANTY and is subject --
  4. -- to the terms of the SATHER LIBRARY GENERAL PUBLIC LICENSE contained in    --
  5. -- the file "Doc/License" of the Sather distribution.  The license is also   --
  6. -- available from ICSI, 1947 Center St., Suite 600, Berkeley CA 94704, USA.  --
  7. --------> Please email comments to "sather-bugs@icsi.berkeley.edu". <----------
  8.  
  9. -- abstract.sa: Abstractions indicating the presence of features.
  10.  
  11. -------------------------------------------------------------------
  12. -- $IS_EQ: Subtypes define "is_eq($OB):BOOL".
  13. -- $IS_LT{T}: Subtypes define "is_lt(T):BOOL".
  14. -- $HASH: Subtypes define "hash:INT".
  15. -- $NIL: Subtypes define "nil:SAME".
  16. -- $COPY: Subtypes define "copy:SAME"
  17. -- $STR: Subtypes define "str:STR".
  18. -- $ELT{T}: Subtypes define "elt!:T".
  19. -------------------------------------------------------------------
  20. partial class COMPARE{ETP} is
  21.    -- Partial class that should be included by containers of elements of
  22.    -- type ETP that need to provide an elt_eq and/or an elt_lt routine.
  23.  
  24.    elt_eq(e1,e2:ETP):BOOL is
  25.       -- The "less than" relation used in the sorting routines. Compares
  26.       -- the object "id" by default. May be redefined in descendants.
  27.       typecase e1
  28.       when $IS_EQ then return e1.is_eq(e2)
  29.       else return SYS::id(e1) = SYS::id(e2) end 
  30.    end;
  31.  
  32.    elt_lt(e1,e2:ETP):BOOL is
  33.       -- The "less than" relation used in the sorting routines. Compares
  34.       -- the object "id" by default. May be redefined in descendants.
  35.       typecase e1
  36.       when $IS_LT{ETP} then return e1.is_lt(e2)
  37.       else return SYS::id(e1) < SYS::id(e2) end 
  38.    end;
  39.  
  40.    elt_hash(e:ETP):INT is
  41.       -- A hash value associated with an element. Must have the property
  42.       -- that if "elt_eq(e1,e2)" then "elt_hash(e1)=elt_hash(e2)". Can
  43.       -- be defined to always return 0, but many routines will then
  44.       -- become quadratic. Uses object "id" by default.
  45.       -- May be redefined in descendants.
  46.       typecase e
  47.       when $HASH then return e.hash
  48.       else return SYS::id(e).hash end 
  49.    end;
  50.  
  51.    elt_nil: ETP is
  52.       -- Return the nil value. If the element is under $NIL then
  53.       -- return e.nil. Otherwise, return void
  54.       -- 
  55.       e: ETP; 
  56.       typecase e 
  57.       when $NIL then 
  58.      -- Note: The double typecase is necessary since
  59.      -- at this point we know that "e" is of type $NIL and
  60.      -- $NIL::nil returns SAME, which is $NIL.
  61.      -- We need the extra typecase to return an ETP.
  62.      -- If ETP < $NIL, then the second typecase is not needed.
  63.      res ::= e.nil; typecase res when ETP then return res end;
  64.       else return void end
  65.    end;      
  66.       
  67.    is_elt_nil(e:ETP):BOOL is
  68.       typecase e
  69.       when $IS_NIL then return e.is_nil;
  70.       else return void(e); end;
  71.    end;
  72.    
  73. end;
  74. -------------------------------------------------------------------
  75. partial class COMPARABLE is
  76.    -- Partial class that implements the generalized equality routine.
  77.    -- Classes should provide an is_eq(SAME) and include this class to
  78.    -- provide the more general versions
  79.    
  80.    stub is_eq(arg: SAME): BOOL;
  81.    
  82.    is_eq(arg: $OB): BOOL is
  83.       typecase arg
  84.       when SAME then return is_eq(arg) 
  85.       else return false end;
  86.    end;
  87.    
  88. end;
  89. -------------------------------------------------------------------
  90. abstract class $IS_EQ is
  91.     -- Subtypes of this define "is_eq:BOOL". Typically used in 
  92.     -- typecases to use instead of "=". Examples: INT < $IS_EQ, 
  93.     -- STR < $IS_EQ.
  94.     -- NOTE:
  95.     --   This equality should be an IMMUTABLE equality that is valid
  96.     --   over the lifetime of the whole object. It should be possible
  97.     --   to use this equality (and an associated hash value) to 
  98.     --   place an object in a hash table and then later retrieve it.
  99.     
  100.     is_eq(e:$OB):BOOL;        -- True if self is equal arg for 
  101.     -- this element type.
  102.  
  103. end;
  104. -------------------------------------------------------------------
  105. abstract class $HASH < $IS_EQ is
  106.    -- Subtypes of this must provide a hash routine.  This is the *new*
  107.    -- $HASH class and is a subtype of $IS_EQ so that all subtypes must
  108.    -- redefine both hash and is_eq. These two routines must work
  109.    -- together - is_eq must refer to an immutable equality
  110.    
  111.    hash: INT;
  112.    
  113. end;
  114. -------------------------------------------------------------------
  115. abstract class $IS_LT{T} < $IS_EQ is
  116.    -- Subtypes of this define "is_lt(T):BOOL and is_eq($OB)". 
  117.    -- Typically used in typecases. 
  118.    -- Examples: INT < $IS_LT{INT}, STR < $IS_LT{INT}.
  119.    -- 
  120.    -- Design Note: $IS_LT has the type parameter since objects are
  121.    -- almost always comparable only with the same type.  With the new
  122.    -- rules, if is_lt were defined on e: $OB, then we would have to
  123.    -- find some meaning for < with arbitrary objects. (you can't just
  124.    -- return false - that makes > be true!).
  125.    -- Hence, objects will only define comparisons with themselves
  126.    -- while the container classes can switch on $IS_LT{T} and then
  127.    -- use the SYS::is_lt when the contained objects are not directly
  128.    -- comparable
  129.    
  130.     
  131.     is_lt(e:T):BOOL;        -- True if self is less than arg for 
  132.     -- this element type.
  133. end;
  134.  
  135. -------------------------------------------------------------------
  136. abstract class $IS_NIL is
  137.     -- Subtypes of this define "is_nil:BOOL". Typically used in typecases.
  138.     -- Example: INT < $IS_NIL.
  139.  
  140.    is_nil:BOOL;         -- Test whether a value is nil.
  141.     -- This is necessary for types with unusual is_eq behavior (such
  142.     -- as IEEE floats).
  143. end;
  144.  
  145. -------------------------------------------------------------------
  146. abstract class $NIL < $IS_NIL is
  147.    -- Inidicates that the subtype provides a nil value
  148.    -- 
  149.    -- Designer's Note:The advantage of T over SAME is that in
  150.    -- parametrized classes typecase e when $NILthen .... e.nil e.nil
  151.    -- is now known to be of type T, whereas if it were SAME, you would
  152.    -- need at least an extra level of typecase
  153.    
  154.    nil:SAME;
  155. end;
  156. -------------------------------------------------------------------
  157. abstract class $COPY is
  158.    -- Indicates that a subtype provides a copy routine
  159.    
  160.     copy:SAME;            -- Return a copy of self.
  161. end;
  162. -------------------------------------------------------------------
  163. abstract class $STR is
  164.     -- Subtypes of this define "str:STR". This should be a reasonable
  165.     -- string representation of an object.
  166.  
  167.     str:STR;            -- String form of object. 
  168. end;
  169.  
  170. -------------------------------------------------------------------
  171. abstract class $ELT is
  172.    -- Subtypes will provide an elt! iterator that returns at least
  173.    -- a $OB (it could be more specific). Most objects will actually
  174.    -- subtype from $ELT{T}
  175.    elt!: $OB;
  176. end;
  177. -------------------------------------------------------------------
  178. abstract class $ELT{T} < $ELT is
  179.    -- Subtypes of this define "elt!:T".  This is a stronger version of
  180.    -- the generic $ELT routine
  181.  
  182.     elt!:T;            -- Contained items.
  183. end;
  184.  
  185. -------------------------------------------------------------------
  186.